package cn.tju.yanbo.commons.anim;

import java.util.Calendar;

import cn.tju.yanbo.utils.AppConfig;
import cn.tju.yanbo.utils.AppUtils;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class RevolveAnimation extends Animation {

    private AnimationController controller = null;
    private Point centroid = null;
    private Rect bounds = null;
    private double revolveDegree = 0;
    private boolean roll = true;
    
    public RevolveAnimation(Point centroid, Rect bounds, double revolveDegree, AnimationController controller) {
        this.centroid = centroid;
        this.bounds = bounds;
        this.revolveDegree = revolveDegree;
        this.controller = controller;
    }
    
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        double degree = revolveDegree*interpolatedTime;
        /*
        double angle = -revolveDegree*interpolatedTime*Math.PI/180;
        double [] revovledPointInTranslatedAxes = AppUtils.revovle(translatedPoint, angle);
        double [] revolvedPoint = new double[]{revovledPointInTranslatedAxes[0]+centroid.x, revovledPointInTranslatedAxes[1]+centroid.y};
        
        */
        double [] leftUpPoint = AppUtils.revolve(bounds, centroid, degree);
        Log.i("revolve", "left="+bounds.left+",right="+bounds.right+",top="+bounds.top+",bottom="+bounds.bottom);
        if(leftUpPoint[1]<=AppConfig.DATE_COMPASS_CENTROID.y) {
//            leftUpPoint = AppUtils.symmetry(leftUpPoint, new double[]{centroid.x, centroid.y});
            if(roll) {
                int type = AppUtils.getPointContainsRing(new Point(bounds.left, bounds.top), centroid, AppConfig.DATE_RING_SERIES_RADIUS);
                switch(type) {
                    case 0 :
                        controller.roll(Calendar.YEAR, degree>0?-1:1);
                        break;
                    case 1 :
                        controller.roll(Calendar.MONTH, degree>0?-1:1);
                        break;
                    case 2 :
                        controller.roll(Calendar.DAY_OF_YEAR, degree>0?-1:1);
                        break;
                    case 3 :
                        controller.roll(Calendar.HOUR_OF_DAY, degree>0?-2:2);
                    default :
                }
                roll = false;
            }
        }
        float dx = (float)(leftUpPoint[0]-bounds.left);
        float dy = (float)(leftUpPoint[1]-bounds.top);
        t.getMatrix().setTranslate(dx, dy);
    }

}
